//初始化函
function init() {
	document.addEventListener("deviceready",mychannelInit, true);
	/*$(document).ready(function(){
		changeTheme(localStorage.theme);
		loadMyChannel();
		
	});*/
}
function mychannelInit(){
    changeTheme(localStorage.theme);
    loadMyChannel();
}
// 读取配置文件中频道信息
function loadMyChannel() {
	$.mobile.showPageLoadingMsg("a", "Loading...", true);
	var success = function(data) {
		data = eval(data);
		var length = data.length;

		// var show = '<ul data-inset="true" data-splittheme="a" >';
		var show = '';
		for ( var i = 0; i < length; i++) {
			show += '<div  class="li_entire"><div class="li_left"><h3>';
			show += data[i].name;
			show += '</h3><p><a href="';
			show += data[i].url;
			show += '" >';
			show += data[i].url;
			show += '</a></p></div><div class="li_center"><div data-role="fieldcontain" align="center"><label for="slider">订阅状态:</label><select name="slider" id="';
			show += data[i].url;
			show += '" onchange="stateChange(this);"><option value="off">未订</option><option value="on">已订</option></select></div></div></div>';
			// show += '</a></p></div><div class="li_center"><div
			// data-role="fieldcontain"><fieldset
			// data-role="controlgroup"><legend>订阅状态</legend><input
			// type="checkbox" name="checkbox-1" id="checkbox-1" class="custom"
			// /><label for="checkbox-1">订阅</label></fieldset></div></div>';
		}
		// show += '</ul>';

		$("#channelList").html(show);
		var myswitch = $("select");
		for ( var i = 0; i < length; i++) {
			if (data[i].enable) {
				myswitch[i].selectedIndex = 1;
			}
		}

		$("#channelList").trigger('create');
		$.mobile.hidePageLoadingMsg();
	};
	var error = function(e) {

	};
	//window.plugins.loadChannnelAndroid01API.loadChannel(success, error, null);
	loadOrder(success);
}
//增加阅读源
function add(){
    var name = $("#input_name").attr("value");
    var rss = $("#input_url").attr("value");
    var channel = localStorage.channel;
    var add= "";
    add+="[{id:'rss',name:'"+name+"',url:'"+rss+"',enable:false},";
    channel = channel.replace("[",add);
    localStorage.setItem("channel",channel);
    $("#channelInput").attr("style","display: none;border:solid");
    loadMyChannel();
}
//show input
function showInput(){
    $("#channelInput").attr("style","display: block;border:solid;border-color:gray;border-radius: 15px 15px 15px 15px;");

}
function loadOrder(success){
	if(localStorage.channel){
		 success(localStorage.channel);
	}else{
		
	}
}
// 更改频道的订阅状态
function stateChange(event) {
	// alert(event.id);
	// alert(event.selectedIndex);
	var url = event.id;
	var state = false;
	if (event.selectedIndex) {
		state = true;
	} else {
		state = false;
	}
	var data =  localStorage.channel;
	data = eval(data);
	var length = data.length;
	var index;
	for ( var i = 0; i < length; i++) {
		if(data[i].url == url){
			index = i;
			//alert(index);
		}
	}
	data[index].enable = state;
	data = JSON.stringify(data);
	localStorage.setItem("channel",data);
}